package dataAnalyze.iteration1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class ImportData {
	
	public static String movieFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/movie_titles.txt";
	public static String trainingSetPath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/training_set/";
	public static String testFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/probe.txt";

	public static Map<Integer, Movie> importMovie(String movieFile)
	{
		Map<Integer, Movie> movies = new HashMap<Integer, Movie>();
		try{
			BufferedReader br = new BufferedReader(new FileReader(movieFile));
			String tempString;
			while ((tempString = br.readLine()) != null)
			{
				String[] s = tempString.split(",");
				Movie m = new Movie();
				m.setId(Integer.parseInt(s[0]));
				if (s[1].equals("NULL")) s[1] = "0";
				m.setYear(Integer.parseInt(s[1]));
				m.setName(s[2]);
				movies.put(Integer.parseInt(s[0]), m);
			}
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return movies;
	}
	
	public static Map<Integer, Vector<Integer>> importRating(String trainingSetPath, Map<Integer, Movie> movies)
	{
		Map<Integer, Vector<Integer>> user_rating_record = new HashMap<Integer, Vector<Integer>>();
		
		File dir = new File(trainingSetPath);
		File[] files = dir.listFiles();
		String tempString = "";
		for (int i = 0; i < 1000; i++)
		{
			File f = files[i];
			System.out.println(f.getName());
			try{
				BufferedReader br = new BufferedReader(new FileReader(f));
				tempString = br.readLine();
				int movie_id = Integer.parseInt(tempString.substring(0, tempString.length()-1));
				Movie m = movies.get(movie_id);
				while ((tempString = br.readLine()) != null)
				{
					String[] s = tempString.split(",");
					Rating rating = new Rating();
					rating.setMovie_id(movie_id);
					int user_id = Integer.parseInt(s[0]);
					rating.setUser_id(user_id);
					rating.setScore(Integer.parseInt(s[1]));
					rating.setDate(s[2]);
					
					m.rates.put(user_id, rating);
					
					if (user_rating_record.containsKey(user_id)){
						Vector<Integer> temp = user_rating_record.get(user_id);
						temp.add(movie_id);
						user_rating_record.put(user_id, temp);
					}else{
						Vector<Integer> temp = new Vector<Integer>();
						temp.add(movie_id);
						user_rating_record.put(user_id, temp);
					}
				}
				br.close();
			}catch(Exception e){
				System.out.println(tempString);
				e.printStackTrace();
			}
		}
		return user_rating_record;
	}
	
	public static Map<Integer, Vector<Integer>> buildTest(String testFile)
	{
		Map<Integer, Vector<Integer>> testSet = new HashMap<Integer, Vector<Integer>>();
		try{
			BufferedReader br = new BufferedReader(new FileReader(testFile));
			
			String tempString = null;
			int movie_id = 0;
			while((tempString = br.readLine()) != null){
				if (tempString.endsWith(":"))
				{
					movie_id = Integer.parseInt(tempString.substring(0,tempString.length()-1));
					if (movie_id > 1000) break;
					tempString = br.readLine();
					Vector<Integer> tempVector = new Vector<Integer>();
					tempVector.add(Integer.parseInt(tempString));
					testSet.put(movie_id, tempVector);
				}else{
					Vector<Integer> tempVector = testSet.get(movie_id);
					tempVector.add(Integer.parseInt(tempString));
				}
			}
			
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return testSet;
	}
}
